/**
 * outsider.com.cn Inc.
 * Copyright (c) 2011-2022 All Rights Reserved.
 */
package com.yuanmeng.common.rabbitmq.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.yuanmeng.common.core.constant.RabbitmqConstants;
import com.yuanmeng.common.core.domain.RabbitMqMessageDTO;

/**
 * @author outsider
 * @version Id: RabbitQueueService.java, v 0.1 2022/11/24 12:04 outsider Exp $$
 */
@Component
public class RabbitQueueService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送订单延迟消息
     * @param messageRouting    消息路由地址
     * @param delayTime         延迟时间 (单位：毫秒)
     * @param object            消息体对象
     */
    public void sendOrderDelayMessage(String messageRouting, Long delayTime, Object object) {

        RabbitMqMessageDTO rabbitMqMessageDTO = RabbitMqMessageDTO.getInstance();
        rabbitMqMessageDTO.setMessageRouting(messageRouting);
        rabbitMqMessageDTO.setDelayTime(delayTime);
        rabbitMqMessageDTO.setMessageObject(JSON.toJSONString(object));

        rabbitTemplate.convertAndSend(RabbitmqConstants.DELAYED_EXCHANGE,
            RabbitmqConstants.ROUTING_KEY, JSON.toJSONString(rabbitMqMessageDTO), a -> {
                a.getMessageProperties().setDelay(rabbitMqMessageDTO.getDelayTime().intValue());
                return a;
            });
        logger.info("发送订单消息至：{}队", RabbitmqConstants.DELAYED_EXCHANGE);
    }

    /**
     * 发送短视频延迟消息
     * @param messageRouting    消息路由地址
     * @param delayTime         延迟时间 (单位：毫秒)
     * @param object            消息体对象
     */
    public void sendVideoDelayMessage(String messageRouting, Long delayTime, Object object) {

        RabbitMqMessageDTO rabbitMqMessageDTO = RabbitMqMessageDTO.getInstance();
        rabbitMqMessageDTO.setMessageRouting(messageRouting);
        rabbitMqMessageDTO.setDelayTime(delayTime);
        rabbitMqMessageDTO.setMessageObject(JSON.toJSONString(object));

        rabbitTemplate.convertAndSend(RabbitmqConstants.VIDEO_DELAYED_EXCHANGE,
            RabbitmqConstants.VIDEO_ROUTING_KEY, JSON.toJSONString(rabbitMqMessageDTO), a -> {
                a.getMessageProperties().setDelay(rabbitMqMessageDTO.getDelayTime().intValue());
                return a;
            });
        logger.info("发送短视频消息至：{}队", RabbitmqConstants.VIDEO_DELAYED_EXCHANGE);
    }

}